<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/18">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/2.0 2.1 流程控制-if条件/12. Python 流程控制-if 条件语句/">Python 流程控制- if 条件语句</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 条件语句是通过一条或多条语句的执行结果（true 或者 false）来决定执行的代码块。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以通过下图来简单了解条件语句的执行过程：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E6%9D%A1%E4%BB%B6%E8%AF%AD%E5%8F%A5/01.jpg?raw=true" alt="iamges"><figcaption class="figure__caption">iamges</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 程序语言指定任何非0或非空（null）值为true，0或者null为false。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 编程中 if 语句用于控制程序的执行，基本形式为：</p>
<ul>
<li>if 语句</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> expression（判断条件）:      </div><div class="line">    statement(s)（执行语句……）</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    statement(s)（执行语句……）</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：Python 使用缩进作为其语句分组的方法，建议使用4个空格。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中“判断条件”成立时（非零），则执行后面的语句，而执行内容可以多行，以缩进来区分表示统一范围。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;else 为可选语句，放需要在条件不成立时，执行内容则可以执行相关语句：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"> </div><div class="line"><span class="comment"># 例1：if 基本用法</span></div><div class="line"> </div><div class="line">flag = <span class="keyword">False</span></div><div class="line">name = <span class="string">'luren'</span></div><div class="line"><span class="keyword">if</span> name == <span class="string">'python'</span>:         <span class="comment"># 判断变量否为'python'</span></div><div class="line">    flag = <span class="keyword">True</span>          <span class="comment"># 条件成立时设置标志为真</span></div><div class="line">    <span class="keyword">print</span> <span class="string">'welcome boss'</span>    <span class="comment"># 并输出欢迎信息</span></div><div class="line"><span class="keyword">else</span>:</div><div class="line">    <span class="keyword">print</span> name              <span class="comment"># 条件不成立时输出变量名称</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果为：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">luren  <span class="comment">#输出结果</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if 语句的判断条件可以用 &gt;（大于）、&lt;（小于）、==（等于）、&gt;=（大于等于）、&lt;=（小于等于）来表示其关系。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当判断条件为多个值时，可以用以下形式：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> expression1（判断条件<span class="number">1</span>）:      </div><div class="line">    statement1(s)（执行语句<span class="number">1</span>……）</div><div class="line"><span class="keyword">elif</span> expression2（判断条件<span class="number">2</span>）:      </div><div class="line">    statement2(s)（执行语句<span class="number">2</span>……）</div><div class="line"><span class="keyword">elif</span> expression3（判断条件<span class="number">3</span>）:      </div><div class="line">    statement3(s)（执行语句<span class="number">3</span>……）</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    statement(s)（执行语句……）</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"><span class="comment"># 例2：elif用法</span></div><div class="line"> </div><div class="line">num = <span class="number">5</span>     </div><div class="line"><span class="keyword">if</span> num == <span class="number">3</span>:            <span class="comment"># 判断num的值</span></div><div class="line">    <span class="keyword">print</span> <span class="string">'boss'</span>        </div><div class="line"><span class="keyword">elif</span> num == <span class="number">2</span>:</div><div class="line">    <span class="keyword">print</span> <span class="string">'user'</span></div><div class="line"><span class="keyword">elif</span> num == <span class="number">1</span>:</div><div class="line">    <span class="keyword">print</span> <span class="string">'worker'</span></div><div class="line"><span class="keyword">elif</span> num &lt; <span class="number">0</span>:           <span class="comment"># 值小于零时输出</span></div><div class="line">    <span class="keyword">print</span> <span class="string">'error'</span></div><div class="line"><span class="keyword">else</span>:</div><div class="line">    <span class="keyword">print</span> <span class="string">'roadman'</span>     <span class="comment"># 条件均不成立时输出</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果为：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">roadman    <span class="comment">#输出结果</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于 python 并不支持 switch 语句，多疑多个条件判断，只能用 elif 来实现，如果判断需要多个条件同时判断时，可以使用 or（或），表示两个条件有一个成立时判断条件成功；使用 and（与）时，表示只有两个条件同时成立的情况下，判断条件才成功。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"> </div><div class="line"><span class="comment"># 例3：if语句多个条件</span></div><div class="line"> </div><div class="line">num = <span class="number">9</span></div><div class="line"><span class="keyword">if</span> num &gt;= <span class="number">0</span> <span class="keyword">and</span> num &lt;= <span class="number">10</span>:    <span class="comment"># 判断值是否在0~10之间</span></div><div class="line">    <span class="keyword">print</span> <span class="string">'hello'</span></div><div class="line"><span class="comment"># 输出结果: hello</span></div><div class="line"> </div><div class="line">num = <span class="number">10</span></div><div class="line"><span class="keyword">if</span> num &lt; <span class="number">0</span> <span class="keyword">or</span> num &gt; <span class="number">10</span>:    <span class="comment"># 判断值是否在小于0或大于10</span></div><div class="line">    <span class="keyword">print</span> <span class="string">'hello'</span></div><div class="line"><span class="keyword">else</span>:</div><div class="line">    <span class="keyword">print</span> <span class="string">'undefine'</span></div><div class="line"><span class="comment"># 输出结果: undefine</span></div><div class="line"> </div><div class="line">num = <span class="number">8</span></div><div class="line"><span class="comment"># 判断值是否在0~5或者10~15之间</span></div><div class="line"><span class="keyword">if</span> (num &gt;= <span class="number">0</span> <span class="keyword">and</span> num &lt;= <span class="number">5</span>) <span class="keyword">or</span> (num &gt;= <span class="number">10</span> <span class="keyword">and</span> num &lt;= <span class="number">15</span>):    </div><div class="line">    <span class="keyword">print</span> <span class="string">'hello'</span></div><div class="line"><span class="keyword">else</span>:</div><div class="line">    <span class="keyword">print</span> <span class="string">'undefine'</span></div><div class="line"><span class="comment"># 输出结果: undefine</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 if 有多个条件时可使用括号来区分判断的先后顺序，括号中的判断优先执行，此外 and 和 or 的优先级低于 &gt;(大于)、&lt;（小于）等判断符号，即大于和小于在没有括号的情况下会比与要优先判断。</p>
<h2 id="简单的语句组"><a href="#简单的语句组" class="headerlink" title="简单的语句组"></a>简单的语句组</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以在同一行的位置上使用 if 条件判断语句：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python </span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"> </div><div class="line">var = <span class="number">100</span> </div><div class="line"> </div><div class="line"><span class="keyword">if</span> ( var  == <span class="number">100</span> ) : <span class="keyword">print</span> <span class="string">"变量 var 的值为100"</span> </div><div class="line"> </div><div class="line"><span class="keyword">print</span> <span class="string">"Good bye!"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行输出结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">变量 var 的值为 <span class="number">100</span></div><div class="line">Good bye！</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.9 字典练习/11. Python 字典练习/">Python 字典练习</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;做一个小练习，当我们在执行脚本的时候，让我们从键盘上读取输入，然后把这些输入存到字典了，再把这个字典打印出来：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vim 2.py</span></div><div class="line"></div><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">info = &#123;&#125;</div><div class="line">name = raw_input(<span class="string">"Please input name:"</span>)</div><div class="line">age = raw_input(<span class="string">"Please input age:"</span>)</div><div class="line">gender = raw_input(<span class="string">'Please input (M/F):'</span>)</div><div class="line">info[<span class="string">'name'</span>] = name</div><div class="line">info[<span class="string">'age'</span>] = age</div><div class="line">info[<span class="string">'gender'</span>] = gender</div><div class="line"><span class="keyword">print</span> info</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以写成</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">info = &#123;&#125;</div><div class="line">name = raw_input(<span class="string">"Please input name:"</span>)</div><div class="line">age = raw_input(<span class="string">"Please input age:"</span>)</div><div class="line">gender = raw_input(<span class="string">'Please input (M/F):'</span>)</div><div class="line">info[<span class="string">'name'</span>] = name</div><div class="line">info[<span class="string">'age'</span>] = age</div><div class="line">info[<span class="string">'gender'</span>] = gender</div><div class="line"><span class="keyword">for</span> k,v <span class="keyword">in</span> info.items():</div><div class="line">    <span class="keyword">print</span> <span class="string">"%s: %s"</span> % (k,v)</div><div class="line"><span class="keyword">print</span> <span class="string">'hello'</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.8 字典/扩展/Python 直接赋值、浅拷贝和深度拷贝解析/">Python 直接赋值、浅拷贝和深度拷贝解析</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-直接赋值、浅拷贝和深度拷贝解析"><a href="#Python-直接赋值、浅拷贝和深度拷贝解析" class="headerlink" title="Python 直接赋值、浅拷贝和深度拷贝解析"></a>Python 直接赋值、浅拷贝和深度拷贝解析</h1><ul>
<li>直接赋值：其实就是对象的引用（别名）。</li>
<li>浅拷贝(copy)：拷贝父对象，不会拷贝对象的内部的子对象。</li>
<li>深拷贝(deepcopy)： copy 模块的 deepcopy 方法，完全拷贝了父对象及其子对象。</li>
</ul>
<h2 id="字典浅拷贝实例"><a href="#字典浅拷贝实例" class="headerlink" title="字典浅拷贝实例"></a>字典浅拷贝实例</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&gt;&gt;&gt;a = &#123;<span class="number">1</span>: [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]&#125;</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>b = a.copy()</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a, b</div><div class="line">(&#123;<span class="number">1</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]&#125;, &#123;<span class="number">1</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]&#125;)</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[<span class="number">1</span>].append(<span class="number">4</span>)</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a, b</div><div class="line">(&#123;<span class="number">1</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]&#125;, &#123;<span class="number">1</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]&#125;)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;深度拷贝需要引入 copy 模块：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&gt;&gt;&gt;<span class="keyword">import</span> copy</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>c = copy.deepcopy(a)</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a, c</div><div class="line">(&#123;<span class="number">1</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]&#125;, &#123;<span class="number">1</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]&#125;)</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[<span class="number">1</span>].append(<span class="number">5</span>)</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a, c</div><div class="line">(&#123;<span class="number">1</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]&#125;, &#123;<span class="number">1</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]&#125;)</div></pre></td></tr></table></figure>
<h2 id="解析"><a href="#解析" class="headerlink" title="解析"></a>解析</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1、<strong>b = a</strong> : 赋值引用，a 和 b 都指向同一个对象。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Python%20%E7%9B%B4%E6%8E%A5%E8%B5%8B%E5%80%BC%E3%80%81%E6%B5%85%E6%8B%B7%E8%B4%9D%E5%92%8C%E6%B7%B1%E5%BA%A6%E6%8B%B7%E8%B4%9D%E8%A7%A3%E6%9E%90/01.png?raw=true" alt="iamges"><figcaption class="figure__caption">iamges</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2、<strong>b = a.copy()</strong> : 浅拷贝, a 和 b 是一个独立的对象，但他们的子对象还是指向统一对象（是引用）。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Python%20%E7%9B%B4%E6%8E%A5%E8%B5%8B%E5%80%BC%E3%80%81%E6%B5%85%E6%8B%B7%E8%B4%9D%E5%92%8C%E6%B7%B1%E5%BA%A6%E6%8B%B7%E8%B4%9D%E8%A7%A3%E6%9E%90/02.png?raw=true" alt="iamges"><figcaption class="figure__caption">iamges</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>b = copy.deepcopy(a)</strong> : 深度拷贝, a 和 b 完全拷贝了父对象及其子对象，两者是完全独立的。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Python%20%E7%9B%B4%E6%8E%A5%E8%B5%8B%E5%80%BC%E3%80%81%E6%B5%85%E6%8B%B7%E8%B4%9D%E5%92%8C%E6%B7%B1%E5%BA%A6%E6%8B%B7%E8%B4%9D%E8%A7%A3%E6%9E%90/03.png?raw=true" alt="iamges"><figcaption class="figure__caption">iamges</figcaption></figure></p>
<h2 id="更多实例"><a href="#更多实例" class="headerlink" title="更多实例"></a>更多实例</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下实例是使用 copy 模块的 copy.copy（ 浅拷贝 ）和（copy.deepcopy ）:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*-coding:utf-8 -*-</span></div><div class="line"> </div><div class="line"><span class="keyword">import</span> copy</div><div class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, [<span class="string">'a'</span>, <span class="string">'b'</span>]] <span class="comment">#原始对象</span></div><div class="line"> </div><div class="line">b = a                       <span class="comment">#赋值，传对象的引用</span></div><div class="line">c = copy.copy(a)            <span class="comment">#对象拷贝，浅拷贝</span></div><div class="line">d = copy.deepcopy(a)        <span class="comment">#对象拷贝，深拷贝</span></div><div class="line"> </div><div class="line">a.append(<span class="number">5</span>)                 <span class="comment">#修改对象a</span></div><div class="line">a[<span class="number">4</span>].append(<span class="string">'c'</span>)            <span class="comment">#修改对象a中的['a', 'b']数组对象</span></div><div class="line"> </div><div class="line">print( <span class="string">'a = '</span>, a )</div><div class="line">print( <span class="string">'b = '</span>, b )</div><div class="line">print( <span class="string">'c = '</span>, c )</div><div class="line">print( <span class="string">'d = '</span>, d )</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例执行输出结果为：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">(<span class="string">'a = '</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>], <span class="number">5</span>])</div><div class="line">(<span class="string">'b = '</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>], <span class="number">5</span>])</div><div class="line">(<span class="string">'c = '</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>]])</div><div class="line">(<span class="string">'d = '</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, [<span class="string">'a'</span>, <span class="string">'b'</span>]])</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.8 字典/10. Python 字典（Dictionary）/">Python 字典（Dictionary）</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-字典"><a href="#Python-字典" class="headerlink" title="Python 字典"></a>Python 字典</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;字典是另一种可变容器模型，切可存储任意类型对象。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;字典是 Python 中唯一的映射类型（哈希表）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;字段对象是可变的，但是字典的键必须使用 <code>可变对象</code> ，一个字典中可以使用不同类型的键值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;字典的每个键值（key=&gt;value）对用冒号(:)分割，每个对之间用逗号(,)分割，整个字典包括在花括号({})中，格式如下所示：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">d = &#123;key1 ： value1,key2 : value2 &#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;键必须是唯一的，但值则不必。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;值可以去任何数据类型，但键必须是不可变的，如字符串，数字或元组。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个简单的字典实例：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">dict = &#123;<span class="string">'Alice'</span>:<span class="string">'2341'</span>, <span class="string">'Beth'</span>: <span class="string">'9102'</span>, <span class="string">'Cecil'</span>: <span class="string">'3258'</span>&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可如此创建字典：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">dict1 = &#123; <span class="string">'abc'</span>: <span class="number">456</span>&#125;;</div><div class="line">dict2 = &#123; <span class="string">'abc'</span>: <span class="number">123</span>, <span class="number">98.6</span><span class="string">" 37 &#125;;</span></div></pre></td></tr></table></figure>
<h2 id="访问字典里的值"><a href="#访问字典里的值" class="headerlink" title="访问字典里的值"></a>访问字典里的值</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把相应的键放入熟悉的方括号</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#/usr/bin/python</span></div><div class="line"></div><div class="line">dict = &#123;<span class="string">'Name'</span>: <span class="string">'Zara'</span>, <span class="string">'Age'</span>: <span class="number">7</span>, <span class="string">'Class'</span>: <span class="string">'First'</span>&#125;;</div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"dict['Name']: "</span>, dict[<span class="string">'Name'</span>];</div><div class="line"><span class="keyword">print</span> <span class="string">"dict['Age']: "</span>, dict[<span class="string">'Age'</span>];</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果为：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">dict[<span class="string">'Name'</span>]:  Zara</div><div class="line">dict[<span class="string">'Age'</span>]:  <span class="number">7</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果用字典里没有的键访问数据，会输出如下错误：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"> </div><div class="line">dict = &#123;<span class="string">'Name'</span>: <span class="string">'Zara'</span>, <span class="string">'Age'</span>: <span class="number">7</span>, <span class="string">'Class'</span>: <span class="string">'First'</span>&#125;;</div><div class="line"> </div><div class="line"><span class="keyword">print</span> <span class="string">"dict['Alice']: "</span>, dict[<span class="string">'Alice'</span>];</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例输出结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">dict[<span class="string">'Alice'</span>]: </div><div class="line">Traceback (most recent call last):</div><div class="line">  File <span class="string">"test.py"</span>, line <span class="number">5</span>, <span class="keyword">in</span> &lt;module&gt;</div><div class="line">    <span class="keyword">print</span> <span class="string">"dict['Alice']: "</span>, dict[<span class="string">'Alice'</span>];</div><div class="line">KeyError: <span class="string">'Alice'</span></div></pre></td></tr></table></figure>
<h2 id="修改字典"><a href="#修改字典" class="headerlink" title="修改字典"></a>修改字典</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;向字典添加新内容的方法是增加新个键/值对，修改或删除已有键/值对：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"> </div><div class="line">dict = &#123;<span class="string">'Name'</span>: <span class="string">'Zara'</span>, <span class="string">'Age'</span>: <span class="number">7</span>, <span class="string">'Class'</span>: <span class="string">'First'</span>&#125;;</div><div class="line"> </div><div class="line">dict[<span class="string">'Age'</span>] = <span class="number">8</span>; <span class="comment"># update existing entry</span></div><div class="line">dict[<span class="string">'School'</span>] = <span class="string">"DPS School"</span>; <span class="comment"># Add new entry</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span class="keyword">print</span> <span class="string">"dict['Age']: "</span>, dict[<span class="string">'Age'</span>];</div><div class="line"><span class="keyword">print</span> <span class="string">"dict['School']: "</span>, dict[<span class="string">'School'</span>];</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">dict[<span class="string">'Age'</span>]:  <span class="number">8</span></div><div class="line">dict[<span class="string">'School'</span>]:  DPS School</div></pre></td></tr></table></figure>
<h2 id="删除字典元素"><a href="#删除字典元素" class="headerlink" title="删除字典元素"></a>删除字典元素</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;能删除单一的元素也能清空字典，清空只需一向操作。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;显示删除一个字典用 del 命令：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"> </div><div class="line">dict = &#123;<span class="string">'Name'</span>: <span class="string">'Zara'</span>, <span class="string">'Age'</span>: <span class="number">7</span>, <span class="string">'Class'</span>: <span class="string">'First'</span>&#125;;</div><div class="line"> </div><div class="line"><span class="keyword">del</span> dict[<span class="string">'Name'</span>]; <span class="comment"># 删除键是'Name'的条目</span></div><div class="line">dict.clear();     <span class="comment"># 清空词典所有条目</span></div><div class="line"><span class="keyword">del</span> dict ;        <span class="comment"># 删除词典</span></div><div class="line"> </div><div class="line"><span class="keyword">print</span> <span class="string">"dict['Age']: "</span>, dict[<span class="string">'Age'</span>];</div><div class="line"><span class="keyword">print</span> <span class="string">"dict['School']: "</span>, dict[<span class="string">'School'</span>];</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;但这会引发一个异常，因为用 del 后字典不再存在：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">dict[<span class="string">'Age'</span>]:</div><div class="line">Traceback (most recent call last):</div><div class="line">  File <span class="string">"test.py"</span>, line <span class="number">8</span>, <span class="keyword">in</span> &lt;module&gt;</div><div class="line">    <span class="keyword">print</span> <span class="string">"dict['Age']: "</span>, dict[<span class="string">'Age'</span>];</div><div class="line">TypeError: <span class="string">'type'</span> object <span class="keyword">is</span> unsubscriptable</div></pre></td></tr></table></figure>
<h2 id="字典键的特性"><a href="#字典键的特性" class="headerlink" title="字典键的特性"></a>字典键的特性</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;字典值可以没有限制地取任何 Python 对象，既可以是标准的对象，也可以是用户定义的，但键不行。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两个重要的点需要记住</p>
<ol>
<li>不允许同一个键出现两次。创建时如果同一个键被赋值两次，后一个值会被记住：</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">dict = &#123;<span class="string">'Name'</span>:<span class="string">'zata'</span>, <span class="string">'Age'</span>: <span class="number">7</span>, <span class="string">'Name'</span>: <span class="string">'Manni'</span>&#125;;</div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"dict['Name']: "</span>, dict[<span class="string">'Name'</span>];</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">dict[<span class="string">'Name'</span>]: Manni</div></pre></td></tr></table></figure>
<ol>
<li>键必须不可变，所以可以用数字，字符串或元组充当，所以用列表就不行：</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"> </div><div class="line">dict = &#123;[<span class="string">'Name'</span>]: <span class="string">'Zara'</span>, <span class="string">'Age'</span>: <span class="number">7</span>&#125;;</div><div class="line"> </div><div class="line"><span class="keyword">print</span> <span class="string">"dict['Name']: "</span>, dict[<span class="string">'Name'</span>];</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">Traceback (most recent call last):</div><div class="line">  File <span class="string">"test.py"</span>, line <span class="number">3</span>, <span class="keyword">in</span> &lt;module&gt;</div><div class="line">    dict = &#123;[<span class="string">'Name'</span>]: <span class="string">'Zara'</span>, <span class="string">'Age'</span>: <span class="number">7</span>&#125;;</div><div class="line">TypeError: list objects are unhashable</div></pre></td></tr></table></figure>
<h2 id="字典内置函数-amp-方法"><a href="#字典内置函数-amp-方法" class="headerlink" title="字典内置函数&amp;方法"></a>字典内置函数&amp;方法</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 字典包含了以下内置函数：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>函数及描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/1.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20cmp(" target="_blank" rel="external">cmp(dict1, dict2)</a>%E6%96%B9%E6%B3%95.md)，较两个字典元素</td>
</tr>
<tr>
<td>2</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/2.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20len(" target="_blank" rel="external">len(dict)</a>%E6%96%B9%E6%B3%95.md)，计算字典元素个数，既键的总和。</td>
</tr>
<tr>
<td>3</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/3.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20str(" target="_blank" rel="external">str(dict)</a>%E6%96%B9%E6%B3%95.md)，输出字典可打印的字符串表示。</td>
</tr>
<tr>
<td>4</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/4.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20type(" target="_blank" rel="external">type(variable)</a>%E6%96%B9%E6%B3%95.md)，返回输入的变量类型，如果变量是字典就返回字典类型。</td>
</tr>
</tbody>
</table>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 字典包含了以下内置方法：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>函数及描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/1.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20clear(" target="_blank" rel="external">dict.clear()</a>%E6%96%B9%E6%B3%95.md)，删除字典内所有元素</td>
</tr>
<tr>
<td>2</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/2.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20copy(" target="_blank" rel="external">dict.copy()</a>%20%E6%96%B9%E6%B3%95.md)，返回一个字典的浅复制</td>
</tr>
<tr>
<td>3</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/3.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20fromkeys(" target="_blank" rel="external">dict.fromkeys(seq[, val]))</a>%E6%96%B9%E6%B3%95.md)，创建一个新字典，以序列 seq 中元素做字典的键，val 为字典所有键对应的初始值</td>
</tr>
<tr>
<td>4</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/4.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20get(" target="_blank" rel="external">dict.get(key, default=None)</a>%E6%96%B9%E6%B3%95.md)，返回指定键的值，如果值不在字典中返回default值</td>
</tr>
<tr>
<td>5</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/5.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20has_key(" target="_blank" rel="external">dict.has_key(key)</a>%E6%96%B9%E6%B3%95.md)，如果键在字典dict里返回true，否则返回false</td>
</tr>
<tr>
<td>6</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/6.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20items(" target="_blank" rel="external">dict.items()</a>%E6%96%B9%E6%B3%95.md)，以列表返回可遍历的(键, 值) 元组数组</td>
</tr>
<tr>
<td>7</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/7.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20keys(" target="_blank" rel="external">dict.keys()</a>%E6%96%B9%E6%B3%95.md)，以列表返回一个字典所有的键</td>
</tr>
<tr>
<td>8</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/8.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20setdefault(" target="_blank" rel="external">dict.setdefault(key, default=None)</a>%E6%96%B9%E6%B3%95.md)，和get()类似, 但如果键不存在于字典中，将会添加键并将值设为default</td>
</tr>
<tr>
<td>9</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/9.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20update(" target="_blank" rel="external">dict.update(dict2)</a>%E6%96%B9%E6%B3%95.md)，把字典dict2的键/值对更新到dict里</td>
</tr>
<tr>
<td>10</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/10.%20Python%20%E5%AD%97%E5%85%B8%EF%BC%88Dictionary%EF%BC%89%20values(" target="_blank" rel="external">dict.values()</a>%E6%96%B9%E6%B3%95.md)，以列表返回字典中的所有值</td>
</tr>
<tr>
<td>11</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/11.%20Python%20%E5%AD%97%E5%85%B8%20pop(" target="_blank" rel="external">pop(key[,default])</a>%E6%96%B9%E6%B3%95.md)，删除字典给定键 key 所对应的值，返回值为被删除的值。key值必须给出。 否则，返回default值。</td>
</tr>
<tr>
<td>12</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/12.%20Python%20%E5%AD%97%E5%85%B8%20popitem(" target="_blank" rel="external">popitem()</a>%E6%96%B9%E6%B3%95.md)，随机返回并删除字典中的一对键和值。</td>
</tr>
</tbody>
</table>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.7 列表/9. Python 的列表(list)/">Python 列表（list）</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;序列是 Python 中最基本的数据结构。序列中的每个元素都分配一个数字-它的位置，或索引，第一个索引就是0，第二个索引是1，依此类推。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 有6个序列的内置类型，但最常见的是列表和元组。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;序列都可以进行的操作包括索引、切片、加、乘、检查成员。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此外，Python 已经内置确定序列的长度以及确定最大和最小的元素的方法。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表是最常用的 Python 的数据类型，它可以作为一个方括号内的逗号分割值出现。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表的数据项不需要具有相同的类型。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表是处理一组有序项目的数据结构，即可以在列表中存储一个序列的项目。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表是可变类型的数据。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个列表，只要把逗号分隔的不同的数据项使用方括号括起来即可。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">49</span>]: list1 = [<span class="string">'python'</span>,<span class="string">'linxu'</span>,<span class="number">1997</span>,<span class="number">2000</span>]</div><div class="line"></div><div class="line">In [<span class="number">50</span>]: list2 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</div><div class="line"></div><div class="line">In [<span class="number">51</span>]: list3 = [<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>,<span class="string">"d"</span>];</div><div class="line">In [<span class="number">52</span>]: list4 = [<span class="string">'a'</span>,<span class="number">1</span>,(<span class="number">4</span>,),[<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>]];</div><div class="line"></div><div class="line">In [<span class="number">53</span>]: type(list1)</div><div class="line">Out[<span class="number">53</span>]: list</div><div class="line"></div><div class="line">In [<span class="number">54</span>]: type(list2)</div><div class="line">Out[<span class="number">54</span>]: list</div><div class="line"></div><div class="line">In [<span class="number">55</span>]: type(list3)</div><div class="line">Out[<span class="number">55</span>]: list</div><div class="line"></div><div class="line">In [<span class="number">56</span>]: type(list4)</div><div class="line">Out[<span class="number">56</span>]: list</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个空的列表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">57</span>]: list5 = []</div><div class="line"></div><div class="line">In [<span class="number">58</span>]: type(list5)</div><div class="line">Out[<span class="number">58</span>]: list</div><div class="line"></div><div class="line">In [<span class="number">59</span>]: list6 = list()</div><div class="line"></div><div class="line">In [<span class="number">60</span>]: type(list6)</div><div class="line">Out[<span class="number">60</span>]: list</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表中追加值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">61</span>]: list5.append(<span class="string">'hello'</span>)</div><div class="line"></div><div class="line">In [<span class="number">62</span>]: list5</div><div class="line">Out[<span class="number">62</span>]: [<span class="string">'hello'</span>]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改列表中一个值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">63</span>]: list4</div><div class="line">Out[<span class="number">63</span>]: [<span class="string">'a'</span>, <span class="number">1</span>, (<span class="number">4</span>,), [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]]</div><div class="line"></div><div class="line">In [<span class="number">64</span>]: list4[<span class="number">0</span>] = <span class="string">'b'</span></div><div class="line"></div><div class="line">In [<span class="number">65</span>]: list4</div><div class="line">Out[<span class="number">65</span>]: [<span class="string">'b'</span>, <span class="number">1</span>, (<span class="number">4</span>,), [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将两个列表变成一个大的列表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">66</span>]: list4 + list5</div><div class="line">Out[<span class="number">66</span>]: [<span class="string">'b'</span>, <span class="number">1</span>, (<span class="number">4</span>,), [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>], <span class="string">'hello'</span>]</div></pre></td></tr></table></figure>
<h2 id="访问列表中的值"><a href="#访问列表中的值" class="headerlink" title="访问列表中的值"></a>访问列表中的值</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用下标索引来访问列表中的值，同样也可以使用方括号的形式截取字符</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">list1 = [<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>];</div><div class="line">list2 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span> ];</div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"list1[0]: "</span>, list1[<span class="number">0</span>]</div><div class="line"><span class="keyword">print</span> <span class="string">"list2[1:5]: "</span>, list2[<span class="number">1</span>:<span class="number">5</span>]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">list[<span class="number">0</span>]: physics</div><div class="line">list2[<span class="number">1</span>:<span class="number">5</span>]: [<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</div></pre></td></tr></table></figure>
<h2 id="更新列表"><a href="#更新列表" class="headerlink" title="更新列表"></a>更新列表</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以对列表的数据项进行修改或更新，也可以使用 append() 方法来添加列表项。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">list = [<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>];</div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"Value available at index 2 : "</span></div><div class="line"><span class="keyword">print</span> list[<span class="number">2</span>];</div><div class="line">list[<span class="number">2</span>] = <span class="number">2001</span>;</div><div class="line"><span class="keyword">print</span> <span class="string">"New value available at index 2 : "</span></div><div class="line"><span class="keyword">print</span> list[<span class="number">2</span>];</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">Value available at index <span class="number">2</span> :</div><div class="line"><span class="number">1997</span></div><div class="line">New value available at index <span class="number">2</span> :</div><div class="line"><span class="number">2001</span></div></pre></td></tr></table></figure>
<h2 id="删除列表元素"><a href="#删除列表元素" class="headerlink" title="删除列表元素"></a>删除列表元素</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以使用 del 语句来删除列表的元素。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">list1 = [<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>];</div><div class="line"></div><div class="line"><span class="keyword">print</span> list1;</div><div class="line"><span class="keyword">del</span> list1[<span class="number">2</span>];</div><div class="line"><span class="keyword">print</span> <span class="string">"After deleting value at index 2 : "</span></div><div class="line"><span class="keyword">print</span> list1;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例输出</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>]</div><div class="line">After deleting value at index <span class="number">2</span> :</div><div class="line">[<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">2000</span>]</div></pre></td></tr></table></figure>
<h2 id="Python-列表脚本操作符"><a href="#Python-列表脚本操作符" class="headerlink" title="Python 列表脚本操作符"></a>Python 列表脚本操作符</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表对 + 和 <em> 的操作符与字符串相似。+ 号用于组合列表， </em> 号用于重复列表。</p>
<table>
<thead>
<tr>
<th>Python表达式</th>
<th>结果</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>len([1,2,3])</td>
<td>3</td>
<td>长度</td>
</tr>
<tr>
<td>[1,2,3] + [4,5,6]</td>
<td>[1,2,3,4,5,6]</td>
<td>组合</td>
</tr>
<tr>
<td>[‘Hi!’] * 4</td>
<td>[‘Hi!’,’Hi!’,’Hi!’,’Hi!’]</td>
<td>重复</td>
</tr>
<tr>
<td>3 in [1,2,3]</td>
<td>true</td>
<td>元素是否存在于列表中</td>
</tr>
<tr>
<td>for x in [1,2,3]:print x,</td>
<td>1 2 3</td>
<td>迭代</td>
</tr>
</tbody>
</table>
<h2 id="Python-列表截取"><a href="#Python-列表截取" class="headerlink" title="Python 列表截取"></a>Python 列表截取</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 的列表截取实例：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>L = [<span class="string">'Google'</span>, <span class="string">'Runoob'</span>, <span class="string">'Taobao'</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>L[<span class="number">2</span>]</div><div class="line"><span class="string">'Taobao'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>L[<span class="number">-2</span>]</div><div class="line"><span class="string">'Runoob'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>L[<span class="number">1</span>:]</div><div class="line">[<span class="string">'Runoob'</span>, <span class="string">'Taobao'</span>]</div><div class="line">&gt;&gt;&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;描述</p>
<table>
<thead>
<tr>
<th>Python 表达式</th>
<th>结果</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>L[2]</td>
<td>‘Taobao’</td>
<td>读取列表中第三个元素</td>
</tr>
<tr>
<td>L[-2]</td>
<td>‘Runoob’</td>
<td>读取列表中倒数第二个元素</td>
</tr>
<tr>
<td>L[1:]</td>
<td>[‘Runoob’,’Taobao’]</td>
<td>从第儿歌元素开始截取列表</td>
</tr>
</tbody>
</table>
<h2 id="Python-列表函数-amp-方法"><a href="#Python-列表函数-amp-方法" class="headerlink" title="Python 列表函数&amp;方法"></a>Python 列表函数&amp;方法</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 包含以下函数：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0/1.%20Python%20List%20cmp(" target="_blank" rel="external">cmp(list1,list2)</a>%E6%96%B9%E6%B3%95.md),比较两个列表的元素</td>
</tr>
<tr>
<td>2</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0/2.%20Python%20List%20len(" target="_blank" rel="external">len(list)</a>%E6%96%B9%E6%B3%95.md),列表元素个数</td>
</tr>
<tr>
<td>3</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0/3.%20Python%20List%20max(" target="_blank" rel="external">max(list)</a>%E6%96%B9%E6%B3%95.md),返回列表元素最大值</td>
</tr>
<tr>
<td>4</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0/4.%20Python%20List%20min(" target="_blank" rel="external">min(list)</a>%E6%96%B9%E6%B3%95.md),返回列表元素最小值</td>
</tr>
<tr>
<td>5</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0/5.%20Python%20List%20list(" target="_blank" rel="external">list(seq)</a>%E6%96%B9%E6%B3%95.md),将元组转换为列表</td>
</tr>
</tbody>
</table>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 包含以下方法</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>方法</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/1.%20Python%20List%20append(" target="_blank" rel="external">list.append(obj)</a>%E6%96%B9%E6%B3%95.md),在列表末尾添加新的对象</td>
</tr>
<tr>
<td>2</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/2.%20Python%20List%20count(" target="_blank" rel="external">list.count(obj)</a>%E6%96%B9%E6%B3%95.md),统计某个元素在列表中出现的次数</td>
</tr>
<tr>
<td>3</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/3.%20Python%20List%20extend(" target="_blank" rel="external">list.extend(seq)</a>%E6%96%B9%E6%B3%95.md),在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）</td>
</tr>
<tr>
<td>4</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/4.%20Python%20List%20index(" target="_blank" rel="external">list.index(obj)</a>%E6%96%B9%E6%B3%95.md),从列表中找出某个值第一个匹配项的索引位置</td>
</tr>
<tr>
<td>5</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/5.%20Python%20List%20insert(" target="_blank" rel="external">list.insert(index, obj)</a>%E6%96%B9%E6%B3%95.md),将对象插入列表</td>
</tr>
<tr>
<td>6</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/6.%20Python%20List%20pop(" target="_blank" rel="external">list.pop(obj=list[-1])</a>%E6%96%B9%E6%B3%95.md),移除列表中的一个元素（默认最后一个元素），并且返回该元素的值</td>
</tr>
<tr>
<td>7</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/7.%20Python%20List%20remove(" target="_blank" rel="external">list.remove(obj)</a>%E6%96%B9%E6%B3%95.md),移除列表中某个值的第一个匹配项</td>
</tr>
<tr>
<td>8</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/8.%20Python%20List%20reverse(" target="_blank" rel="external">list.reverse()</a>%E6%96%B9%E6%B3%95.md),反向列表中元素</td>
</tr>
<tr>
<td>9</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/9.%20Python%20List%20sort(" target="_blank" rel="external">list.sort([func])</a>%E6%96%B9%E6%B3%95.md),对厡列表进行排序</td>
</tr>
</tbody>
</table>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.6 元组/8. Python 元组/">Python 元组</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-元组"><a href="#Python-元组" class="headerlink" title="Python 元组"></a>Python 元组</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组是序列的一种。序列包括字符串、列表和元组。</p>
<h2 id="序列说明"><a href="#序列说明" class="headerlink" title="序列说明"></a>序列说明</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;序列的两个主要特点是索引操作符和切片操作符</p>
<ul>
<li>索引操作符让我们可以从序列中抓取一个特定项目</li>
<li>切片操作符让我们能够获取序列的一个切片，即一部分序列。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;序列的基本操作</p>
<ul>
<li>len()：求序列的长度</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">1</span>]: a = <span class="string">'abc'</span></div><div class="line"></div><div class="line">In [<span class="number">2</span>]: len(a)</div><div class="line">Out[<span class="number">2</span>]: <span class="number">3</span></div></pre></td></tr></table></figure>
<ul>
<li>+：连接2个序列</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">3</span>]: a + <span class="string">'d'</span></div><div class="line">Out[<span class="number">3</span>]: <span class="string">'abcd'</span></div></pre></td></tr></table></figure>
<ul>
<li>*：重复序列元素</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">4</span>]: a</div><div class="line">Out[<span class="number">4</span>]: <span class="string">'abc'</span></div><div class="line"></div><div class="line">In [<span class="number">5</span>]: a * <span class="number">4</span></div><div class="line">Out[<span class="number">5</span>]: <span class="string">'abcabcabcabc'</span></div><div class="line"></div><div class="line">In [<span class="number">8</span>]: <span class="string">'@'</span> * <span class="number">10</span></div><div class="line">Out[<span class="number">8</span>]: <span class="string">'@@@@@@@@@@'</span></div></pre></td></tr></table></figure>
<ul>
<li>in 或 not in：判断元素是否存在序列中</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">9</span>]: <span class="string">'a'</span> <span class="keyword">in</span> a</div><div class="line">Out[<span class="number">9</span>]: <span class="keyword">True</span></div><div class="line"></div><div class="line">In [<span class="number">10</span>]: a</div><div class="line">Out[<span class="number">10</span>]: <span class="string">'abc'</span></div><div class="line"></div><div class="line">In [<span class="number">11</span>]: <span class="string">'d'</span> <span class="keyword">in</span> a</div><div class="line">Out[<span class="number">11</span>]: <span class="keyword">False</span></div><div class="line"></div><div class="line">In [<span class="number">12</span>]: <span class="string">'d'</span> <span class="keyword">not</span> <span class="keyword">in</span> a</div><div class="line">Out[<span class="number">12</span>]: <span class="keyword">True</span></div></pre></td></tr></table></figure>
<ul>
<li>max()：返回最大值</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">13</span>]: max(a)</div><div class="line">Out[<span class="number">13</span>]: <span class="string">'c'</span></div></pre></td></tr></table></figure>
<ul>
<li>min()：返回最小值</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">14</span>]: min(a)</div><div class="line">Out[<span class="number">14</span>]: <span class="string">'a'</span></div></pre></td></tr></table></figure>
<ul>
<li>cmp(x,y)：比较两个个序列是否相等</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">15</span>]: a</div><div class="line">Out[<span class="number">15</span>]: <span class="string">'abc'</span></div><div class="line"></div><div class="line">In [<span class="number">16</span>]: cmp(a,<span class="string">'abc'</span>)</div><div class="line">Out[<span class="number">16</span>]: <span class="number">0</span></div><div class="line"></div><div class="line">In [<span class="number">17</span>]: cmp(a,<span class="string">'abcd'</span>)</div><div class="line">Out[<span class="number">17</span>]: <span class="number">-1</span></div><div class="line"></div><div class="line">In [<span class="number">18</span>]: cmp(a,<span class="string">'ab'</span>)</div><div class="line">Out[<span class="number">18</span>]: <span class="number">1</span></div><div class="line"></div><div class="line">In [<span class="number">19</span>]: cmp(a+<span class="string">'g'</span>,<span class="string">'abcdef'</span>)</div><div class="line">Out[<span class="number">19</span>]: <span class="number">1</span></div></pre></td></tr></table></figure>
<h2 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 的元组与列表十分相似，不同之处在于元组的元素不能修改。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组和字符串一样是不可变的</p>
<ul>
<li>元组可以存储一系列的值</li>
<li>元组通常用在用户定义的函数能够安全地采用一组值的时候，即被使用的元组的值不回改变</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组使用小括号，列表使用方括号。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">21</span>]: tup1 = (<span class="string">'python'</span>,<span class="string">'a'</span>,<span class="number">1997</span>,<span class="number">2017</span>);</div><div class="line"></div><div class="line">In [<span class="number">22</span>]: type(tup1)</div><div class="line">Out[<span class="number">22</span>]: tuple</div><div class="line"></div><div class="line">In [<span class="number">23</span>]: tup2 = (<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,(<span class="number">5</span>,))</div><div class="line"></div><div class="line">In [<span class="number">24</span>]: type(tup2)</div><div class="line">Out[<span class="number">24</span>]: tuple</div><div class="line"></div><div class="line">In [<span class="number">25</span>]: tup3 = <span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>,<span class="string">"d"</span>;</div><div class="line"></div><div class="line">In [<span class="number">26</span>]: type(tup3)</div><div class="line">Out[<span class="number">26</span>]: tuple</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建空元组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">27</span>]: tup1 = ();</div><div class="line"></div><div class="line">In [<span class="number">28</span>]: type(tup1)</div><div class="line">Out[<span class="number">28</span>]: tuple</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组中只包含一个元素时，需要在元素后面添加逗号</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">29</span>]: tup1 = (<span class="number">50</span>,);</div><div class="line"></div><div class="line">In [<span class="number">30</span>]: type(tup1)</div><div class="line">Out[<span class="number">30</span>]: tuple</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组与字符串类似，下标索引从0开始，可以进行截取，组合等。</p>
<h2 id="访问元组"><a href="#访问元组" class="headerlink" title="访问元组"></a>访问元组</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组可以使用下标索引来访问元组中的值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">tup1 = (<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>);</div><div class="line">tup2 = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span> );</div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"tup1[0]: "</span>, tup1[<span class="number">0</span>]</div><div class="line"><span class="keyword">print</span> <span class="string">"tup2[1:5]: "</span>, tup2[<span class="number">1</span>:<span class="number">5</span>]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">tup1[<span class="number">0</span>]:  physics</div><div class="line">tup2[<span class="number">1</span>:<span class="number">5</span>]:  (<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</div></pre></td></tr></table></figure>
<h2 id="修改元组"><a href="#修改元组" class="headerlink" title="修改元组"></a>修改元组</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组的元素值是不允许修改的，但可以对元组进行连接组合</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"></div><div class="line">tup1 = (<span class="number">12</span>, <span class="number">34.56</span>);</div><div class="line">tup2 = (<span class="string">'abc'</span>, <span class="string">'xyz'</span>);</div><div class="line"></div><div class="line"><span class="comment"># 以下修改元组元素操作是非法的。</span></div><div class="line"><span class="comment"># tup1[0] = 100;</span></div><div class="line"></div><div class="line"><span class="comment"># 创建一个新的元组</span></div><div class="line">tup3 = tup1 + tup2;</div><div class="line"><span class="keyword">print</span> tup3;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">(<span class="number">12</span>,<span class="number">34</span>,<span class="number">56</span>,<span class="string">'abc'</span>,<span class="string">'xyz'</span>)</div></pre></td></tr></table></figure>
<h2 id="删除元组"><a href="#删除元组" class="headerlink" title="删除元组"></a>删除元组</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组中的元素值是不允许删除的，但可以使用 del 语句来删除整个元组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">tup = (<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>);</div><div class="line"></div><div class="line"><span class="keyword">print</span> tup;</div><div class="line"><span class="keyword">del</span> tup;</div><div class="line"><span class="keyword">print</span> <span class="string">"After deleting tup : "</span></div><div class="line"><span class="keyword">print</span> tup;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例元组被删除后，输出变量会有异常信息，输出如下所示：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">(<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>)</div><div class="line">After deleting tup :</div><div class="line">Traceback (most recent call last):</div><div class="line">  File <span class="string">"test.py"</span>, line <span class="number">9</span>, <span class="keyword">in</span> &lt;module&gt;</div><div class="line">    <span class="keyword">print</span> tup;</div><div class="line">NameError: name <span class="string">'tup'</span> <span class="keyword">is</span> <span class="keyword">not</span> defined</div></pre></td></tr></table></figure>
<h2 id="元组运算符"><a href="#元组运算符" class="headerlink" title="元组运算符"></a>元组运算符</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;与字符串一样，元组之间可以使用 + 号和 <em> 号进行元算。这就意味着它们可以组合和复制，运算后会生成一个新的元组。<br>python表达式|结果|描述<br>—|—|—<br>len((1,2,3))|3|计算元素个数<br>（1，2，3） + （4，5，6）|（1，2，3，4，5，6）|连接<br>(‘Hi!’) </em> 4|(‘Hi!’,’Hi!’,’Hi!’,’Hi!’)|复制<br>3 in (1,2,3)|true|元素是否存在<br>for x in (1,2,3):print x,|1 2 3 |迭代</p>
<h2 id="元组索引、截取"><a href="#元组索引、截取" class="headerlink" title="元组索引、截取"></a>元组索引、截取</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为元组也是一个序列，所以可以访问元组中的指定位置的元素，也可以截取索引中的一段元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">L = (<span class="string">'spam'</span>,<span class="string">'Spam'</span>,<span class="string">'SPAM!'</span>)</div></pre></td></tr></table></figure>
<table>
<thead>
<tr>
<th>python表达式</th>
<th>结果</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>L[2]</td>
<td>‘SPANM!’</td>
<td>读取第三个元素</td>
</tr>
<tr>
<td>L[-2]</td>
<td>‘Spam’</td>
<td>反向读取；读取倒数第二个元素</td>
</tr>
<tr>
<td>L[1:]</td>
<td>(‘Spam’,’SPAM!’)</td>
<td>截取元素</td>
</tr>
</tbody>
</table>
<h2 id="无关闭分隔符"><a href="#无关闭分隔符" class="headerlink" title="无关闭分隔符"></a>无关闭分隔符</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;任意无符号的对象，以逗号隔开，默认为元组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">'abc'</span>, <span class="number">-4.24e93</span>, <span class="number">18</span>+<span class="number">6.6j</span>, <span class="string">'xyz'</span>;</div><div class="line">x, y = <span class="number">1</span>, <span class="number">2</span>;</div><div class="line"><span class="keyword">print</span> <span class="string">"Value of x , y : "</span>, x,y;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例运行结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">abc <span class="number">-4.24e+93</span> (<span class="number">18</span>+<span class="number">6.6j</span>) xyz</div><div class="line">Value of x , y : <span class="number">1</span> <span class="number">2</span></div></pre></td></tr></table></figure>
<h2 id="元组内置函数"><a href="#元组内置函数" class="headerlink" title="元组内置函数"></a>元组内置函数</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;python元组包含了以下内置函数</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>方法及描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/%E5%85%83%E7%BB%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/1.%20Python%20Tuple(%E5%85%83%E7%BB%84" target="_blank" rel="external">cmp(tuple1, tuple2)</a>%20cmp()%E6%96%B9%E6%B3%95.md)，比较两个元组元素。</td>
</tr>
<tr>
<td>2</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/%E5%85%83%E7%BB%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/2.%20Python%20Tuple(%E5%85%83%E7%BB%84" target="_blank" rel="external">len(tuple)</a>%20len()%E6%96%B9%E6%B3%95.md)，计算元组元素个数。</td>
</tr>
<tr>
<td>3</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/%E5%85%83%E7%BB%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/3.%20Python%20Tuple(%E5%85%83%E7%BB%84" target="_blank" rel="external">max(tuple)</a>%20max()%E6%96%B9%E6%B3%95.md)，返回元组中元素最大值。</td>
</tr>
<tr>
<td>4</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/%E5%85%83%E7%BB%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/4.%20Python%20Tuple(%E5%85%83%E7%BB%84" target="_blank" rel="external">min(tuple)</a>%20min()%E6%96%B9%E6%B3%95.md)，返回元组中元素最小值。</td>
</tr>
<tr>
<td>5</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/%E5%85%83%E7%BB%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/5.%20Python%20Tuple(%E5%85%83%E7%BB%84" target="_blank" rel="external">tuple(seq)</a>%20tuple()%E6%96%B9%E6%B3%95.md)，将列表转换为元组。</td>
</tr>
</tbody>
</table>
<h2 id="元组的拆分"><a href="#元组的拆分" class="headerlink" title="元组的拆分"></a>元组的拆分</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以通过变量去接收元组的值,通常叫做元组的拆分</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">31</span>]: tup1 = (a,<span class="string">'b'</span>,<span class="string">'c'</span>)</div><div class="line"></div><div class="line">In [<span class="number">32</span>]: tup1</div><div class="line">Out[<span class="number">32</span>]: (<span class="string">'abc'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>)</div><div class="line"></div><div class="line">In [<span class="number">33</span>]: first,second,third = tup1</div><div class="line"></div><div class="line">In [<span class="number">34</span>]: first</div><div class="line">Out[<span class="number">34</span>]: <span class="string">'abc'</span></div><div class="line"></div><div class="line">In [<span class="number">35</span>]: second</div><div class="line">Out[<span class="number">35</span>]: <span class="string">'b'</span></div><div class="line"></div><div class="line">In [<span class="number">36</span>]: third</div><div class="line">Out[<span class="number">36</span>]: <span class="string">'c'</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.5 Python 的数值和字符串/7. Python 字符串/">Python 字符串</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-字符串"><a href="#Python-字符串" class="headerlink" title="Python 字符串"></a>Python 字符串</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;字符串是 Python 中最常用的数据类型。可以使用引号（‘’或“”）来创建字符串。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建字符串很简单，只要为变量分配一个值即可。例如：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">var1 = <span class="string">'hello world'</span></div><div class="line">var2 = <span class="string">"python runoob"</span></div></pre></td></tr></table></figure>
<h2 id="Python-访问字符串中的值"><a href="#Python-访问字符串中的值" class="headerlink" title="Python 访问字符串中的值"></a>Python 访问字符串中的值</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 不支持单字符类型，单字符在 Python 也是作为一个字符串使用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 访问子字符串，可以使用方括号来截取字符串，如下实例：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">var1 = <span class="string">'Hello World!'</span></div><div class="line">var2 = <span class="string">"Python Runoob"</span></div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"var1[0]: "</span>, var1[<span class="number">0</span>]</div><div class="line"><span class="keyword">print</span> <span class="string">"var2[1:5]: "</span>, var2[<span class="number">1</span>:<span class="number">5</span>]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例执行结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">var1[<span class="number">0</span>]:  H</div><div class="line">var2[<span class="number">1</span>:<span class="number">5</span>]:   ytho</div></pre></td></tr></table></figure>
<h2 id="Python-字符串更新"><a href="#Python-字符串更新" class="headerlink" title="Python 字符串更新"></a>Python 字符串更新</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以对已存在的字符串进行修改，并赋值给另一个变量，如下实例：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"></div><div class="line">var1 = <span class="string">'Hello World!'</span></div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"更新字符串 :- "</span>, var1[:<span class="number">6</span>] + <span class="string">'Runoob!'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例执行结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">更新字符串 ：- Hello Runoob!</div></pre></td></tr></table></figure>
<h2 id="Python-转义字符"><a href="#Python-转义字符" class="headerlink" title="Python 转义字符"></a>Python 转义字符</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当需要在字符中使用特殊字符时，Python 用反斜杠（\）转义字符。如下表：</p>
<table>
<thead>
<tr>
<th>转义字符</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>\（在行尾时）</td>
<td>续行符</td>
</tr>
<tr>
<td>\\</td>
<td>反斜杠符号</td>
</tr>
<tr>
<td>\‘</td>
<td>反引号</td>
</tr>
<tr>
<td>\“</td>
<td>双引号</td>
</tr>
<tr>
<td>\a</td>
<td>响铃</td>
</tr>
<tr>
<td>\b</td>
<td>退格（Backspace）</td>
</tr>
<tr>
<td>\e</td>
<td>转义</td>
</tr>
<tr>
<td>\000</td>
<td>空</td>
</tr>
<tr>
<td>\n</td>
<td>换行</td>
</tr>
<tr>
<td>\v</td>
<td>纵向制表符</td>
</tr>
<tr>
<td>\t</td>
<td>横向制表符</td>
</tr>
<tr>
<td>\r</td>
<td>回车</td>
</tr>
<tr>
<td>\f</td>
<td>换页</td>
</tr>
<tr>
<td>\oyy</td>
<td>八进制数，yy代表的字符，例如：\o12 代表换行</td>
</tr>
<tr>
<td>\xyy</td>
<td>十六进制数，yy代表的字符，例如：\x0a代表换行</td>
</tr>
<tr>
<td>\other</td>
<td>其他的字符以普通格式输出</td>
</tr>
</tbody>
</table>
<h2 id="Python-字符串运算符"><a href="#Python-字符串运算符" class="headerlink" title="Python 字符串运算符"></a>Python 字符串运算符</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下表实例变量 a 值为字符串 “Hello”，b 变量为 “Python”：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E5%AD%97%E7%AC%A6%E4%B8%B2/01.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"></div><div class="line">a = <span class="string">"Hello"</span></div><div class="line">b = <span class="string">"Python"</span></div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"a + b 输出结果："</span>, a + b </div><div class="line"><span class="keyword">print</span> <span class="string">"a * 2 输出结果："</span>, a * <span class="number">2</span> </div><div class="line"><span class="keyword">print</span> <span class="string">"a[1] 输出结果："</span>, a[<span class="number">1</span>] </div><div class="line"><span class="keyword">print</span> <span class="string">"a[1:4] 输出结果："</span>, a[<span class="number">1</span>:<span class="number">4</span>] </div><div class="line"></div><div class="line"><span class="keyword">if</span>( <span class="string">"H"</span> <span class="keyword">in</span> a) :</div><div class="line">    <span class="keyword">print</span> <span class="string">"H 在变量 a 中"</span> </div><div class="line"><span class="keyword">else</span> :</div><div class="line">	<span class="keyword">print</span> <span class="string">"H 不在变量 a 中"</span> </div><div class="line"></div><div class="line"><span class="keyword">if</span>( <span class="string">"M"</span> <span class="keyword">not</span> <span class="keyword">in</span> a) :</div><div class="line">    <span class="keyword">print</span> <span class="string">"M 不在变量 a 中"</span> </div><div class="line"><span class="keyword">else</span> :</div><div class="line">	<span class="keyword">print</span> <span class="string">"M 在变量 a 中"</span></div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">r'\n'</span></div><div class="line"><span class="keyword">print</span> R<span class="string">'\n'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上程序执行结果为：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">a + b 输出结果： HelloPython</div><div class="line">a * <span class="number">2</span> 输出结果： HelloHello</div><div class="line">a[<span class="number">1</span>] 输出结果： e</div><div class="line">a[<span class="number">1</span>:<span class="number">4</span>] 输出结果： ell</div><div class="line">H 在变量 a 中</div><div class="line">M 不在变量 a 中</div><div class="line">\n</div><div class="line">\n</div></pre></td></tr></table></figure>
<h2 id="Python-字符串格式化"><a href="#Python-字符串格式化" class="headerlink" title="Python 字符串格式化"></a>Python 字符串格式化</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 支持格式化字符串的输出，尽管这样可能会用到非常复杂发表达式，但最基本的用法是将一个值插入到有一个有字符串格式符 %s 发字符串中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 Python 中，字符串格式化使用与 C 中 sprintf函数一样的语法。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如下实例：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"My name is %s and weight is %d kg!"</span> % (<span class="string">'Zara'</span>,<span class="number">21</span>)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例输出结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">My name <span class="keyword">is</span> Zara <span class="keyword">and</span> weight <span class="keyword">is</span> <span class="number">21</span> kg!</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;python 字符串格式化符号：</p>
<table>
<thead>
<tr>
<th>符号</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>%c</td>
<td>格式化字符及其ASCII码</td>
</tr>
<tr>
<td>%s</td>
<td>格式化字符串</td>
</tr>
<tr>
<td>%d</td>
<td>格式化整数</td>
</tr>
<tr>
<td>%u</td>
<td>格式化无符号整型</td>
</tr>
<tr>
<td>%o</td>
<td>格式化无符号八进制数</td>
</tr>
<tr>
<td>%x</td>
<td>格式化无符号十六进制数</td>
</tr>
<tr>
<td>%X</td>
<td>格式化无符号十六进制数（大写）</td>
</tr>
<tr>
<td>%f</td>
<td>格式化浮点数字，可指定小数点后的精度</td>
</tr>
<tr>
<td>%e</td>
<td>用科学计数法格式化浮点数</td>
</tr>
<tr>
<td>%E</td>
<td>作用同 %e ，用科学计数法格式化浮点数</td>
</tr>
<tr>
<td>%g</td>
<td>%f 和 %e 的简写</td>
</tr>
<tr>
<td>%G</td>
<td>%f 和 %E 的简写</td>
</tr>
<tr>
<td>%p</td>
<td>用十六进制数格式化变量的地址</td>
</tr>
</tbody>
</table>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式化操作符辅助指令：</p>
<table>
<thead>
<tr>
<th>符号</th>
<th>功能</th>
</tr>
</thead>
<tbody>
<tr>
<td>*</td>
<td>定义宽度或者小数点精度</td>
</tr>
<tr>
<td>-</td>
<td>用作左对齐</td>
</tr>
<tr>
<td>+</td>
<td>在正数前面显示加号（+）</td>
</tr>
<tr>
<td><sp></sp></td>
<td>在正数前面显示空格</td>
</tr>
<tr>
<td>#</td>
<td>在八进制数前面显示零（‘0’），在十六进制数前显示‘0x’或者‘0X’（取决于用的是‘x’还是‘X’）</td>
</tr>
<tr>
<td>0</td>
<td>显示的数字前面填充‘0’而不是默认的空格</td>
</tr>
<tr>
<td>%</td>
<td>‘%%’输出一个单一的‘%’</td>
</tr>
<tr>
<td>（var）</td>
<td>映射变量（字典参数）</td>
</tr>
<tr>
<td>m.n.</td>
<td>m 是显示的最小总宽度，n 是小数点后的位数(如果可用的话)</td>
</tr>
</tbody>
</table>
<h2 id="Python-三引号（triple-quotes）"><a href="#Python-三引号（triple-quotes）" class="headerlink" title="Python 三引号（triple quotes）"></a>Python 三引号（triple quotes）</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;python 中三引号可以将复杂的字符串进行复制：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;python 三引号允许一个字符串跨多行，字符串中可以包含换行符、制表符以及其他特殊字符。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;三引号的语法是一对连续的单引号或者双引号（通常都是成对的用）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>hi = <span class="string">'''hi</span></div><div class="line"><span class="string">there'''</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>hi <span class="comment">#repr()</span></div><div class="line"><span class="string">'hi\nthere'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">print</span> hi <span class="comment"># str()</span></div><div class="line">hi</div><div class="line">there</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;三引号让程序员从引号和特殊字符串的泥潭里面解脱出来，自始至终宝石一小块字符串的格式是所谓的WYSIWYG（所见即所得）格式的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个典型的用例是，当需要一块 HTML 或者SQL时，这是用字符串组合，特殊字符串转义将会非常的繁琐。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">errHTML = <span class="string">'''</span></div><div class="line"><span class="string">&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;</span></div><div class="line"><span class="string">Friends CGI Demo&lt;/TITLE&gt;&lt;/HEAD&gt;</span></div><div class="line"><span class="string">&lt;BODY&gt;&lt;H3&gt;ERROR&lt;/H3&gt;</span></div><div class="line"><span class="string">&lt;B&gt;%s&lt;/B&gt;&lt;P&gt;</span></div><div class="line"><span class="string">&lt;FORM&gt;&lt;INPUT TYPE=button VALUE=Back</span></div><div class="line"><span class="string">ONCLICK="window.history.back()"&gt;&lt;/FORM&gt;</span></div><div class="line"><span class="string">&lt;/BODY&gt;&lt;/HTML&gt;</span></div><div class="line"><span class="string">'''</span></div><div class="line">cursor.execute(<span class="string">'''</span></div><div class="line"><span class="string">CREATE TABLE users (  </span></div><div class="line"><span class="string">login VARCHAR(8), </span></div><div class="line"><span class="string">uid INTEGER,</span></div><div class="line"><span class="string">prid INTEGER)</span></div><div class="line"><span class="string">'''</span>)</div></pre></td></tr></table></figure>
<h2 id="Unicode-字符串"><a href="#Unicode-字符串" class="headerlink" title="Unicode 字符串"></a>Unicode 字符串</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 中定义一个 Unicode 字符串和定义一个普通字符串一样简单：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">u'Hello World !'</span></div><div class="line"><span class="string">u'Hello World !'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;引号前小写的”u”表示这里创建的是一个 Unicode 字符串。如果你想加入一个特殊字符，可以使用 Python 的 Unicode-Escape 编码。如下例所示：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">u'Hello\u0020World !'</span></div><div class="line"><span class="string">u'Hello World !'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;被替换的 \u0020 标识表示在给定位置插入编码值为 0x0020 的 Unicode 字符（空格符）。</p>
<h2 id="python-的字符串内建函数"><a href="#python-的字符串内建函数" class="headerlink" title="python 的字符串内建函数"></a>python 的字符串内建函数</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;字符串方法是从python1.6到2.0慢慢加进来的——它们也被加到了Jython中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这些方法实现了string模块的大部分方法，如下表所示列出了目前字符串内建支持的方法，所有的方法都包含了对Unicode的支持，有一些甚至是专门用于Unicode的。</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>string.capitalize()</td>
<td>把字符串的第一个字符大写</td>
</tr>
<tr>
<td>string.center(width)</td>
<td>返回一个原字符串居中,并使用空格填充至长度 width 的新字符串</td>
</tr>
<tr>
<td>string.count(str, beg=0, end=len(string))</td>
<td>返回 str 在 string 里面出现的次数，如果 beg 或者 end 指定则返回指定范围内 str 出现的次数</td>
</tr>
<tr>
<td>string.decode(encoding=’UTF-8’, errors=’strict’)</td>
<td>以 encoding 指定的编码格式解码 string，如果出错默认报一个 ValueError 的 异 常 ， 除 非 errors 指 定 的 是 ‘ignore’ 或 者’replace’</td>
</tr>
<tr>
<td>string.encode(encoding=’UTF-8’, errors=’strict’)</td>
<td>以 encoding 指定的编码格式编码 string，如果出错默认报一个ValueError 的异常，除非 errors 指定的是’ignore’或者’replace’</td>
</tr>
<tr>
<td>string.endswith(obj, beg=0, end=len(string))</td>
<td>检查字符串是否以 obj 结束，如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束，如果是，返回 True,否则返回 False.</td>
</tr>
<tr>
<td>string.expandtabs(tabsize=8)</td>
<td>把字符串 string 中的 tab 符号转为空格，tab 符号默认的空格数是 8。</td>
</tr>
<tr>
<td>string.find(str, beg=0, end=len(string))</td>
<td>检测 str 是否包含在 string 中，如果 beg 和 end 指定范围，则检查是否包含在指定范围内，如果是返回开始的索引值，否则返回-1</td>
</tr>
<tr>
<td>string.format()</td>
<td>格式化字符串</td>
</tr>
<tr>
<td>string.index(str, beg=0, end=len(string))</td>
<td>跟find()方法一样，只不过如果str不在 string中会报一个异常.</td>
</tr>
<tr>
<td>string.isalnum()</td>
<td>如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False</td>
</tr>
<tr>
<td>string.isalpha()</td>
<td>如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False</td>
</tr>
<tr>
<td>string.isdecimal()</td>
<td>如果 string 只包含十进制数字则返回 True 否则返回 False.</td>
</tr>
<tr>
<td>string.isdigit()</td>
<td>如果 string 只包含数字则返回 True 否则返回 False.</td>
</tr>
<tr>
<td>string.islower()</td>
<td>如果 string 中包含至少一个区分大小写的字符，并且所有这些(区分大小写的)字符都是小写，则返回 True，否则返回 False</td>
</tr>
<tr>
<td>string.isnumeric()</td>
<td>如果 string 中只包含数字字符，则返回 True，否则返回 False</td>
</tr>
<tr>
<td>string.isspace()</td>
<td>如果 string 中只包含空格，则返回 True，否则返回 False.</td>
</tr>
<tr>
<td>string.istitle()</td>
<td>如果 string 是标题化的(见 title())则返回 True，否则返回 False</td>
</tr>
<tr>
<td>string.isupper()</td>
<td>如果 string 中包含至少一个区分大小写的字符，并且所有这些(区分大小写的)字符都是大写，则返回 True，否则返回 False</td>
</tr>
<tr>
<td>string.join(seq)</td>
<td>以 string 作为分隔符，将 seq 中所有的元素(的字符串表示)合并为一个新的字符串</td>
</tr>
<tr>
<td>string.ljust(width)</td>
<td>返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串</td>
</tr>
<tr>
<td>string.lower()</td>
<td>转换 string 中所有大写字符为小写.</td>
</tr>
<tr>
<td>string.lstrip()</td>
<td>截掉 string 左边的空格</td>
</tr>
<tr>
<td>string.maketrans(intab, outtab])</td>
<td>maketrans() 方法用于创建字符映射的转换表，对于接受两个参数的最简单的调用方式，第一个参数是字符串，表示需要转换的字符，第二个参数也是字符串表示转换的目标。</td>
</tr>
<tr>
<td>max(str)</td>
<td>返回字符串 str 中最大的字母。</td>
</tr>
<tr>
<td>min(str)</td>
<td>返回字符串 str 中最小的字母。</td>
</tr>
<tr>
<td>string.partition(str)</td>
<td>有点像 find()和 split()的结合体,从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个 3 元 素 的 元 组 (string_pre_str,str,string_post_str),如果 string 中不包含str 则 string_pre_str == string.</td>
</tr>
<tr>
<td>string.replace(str1, str2,  num=string.count(str1))</td>
<td>把 string 中的 str1 替换成 str2,如果 num 指定，则替换不超过 num 次.</td>
</tr>
<tr>
<td>string.rfind(str, beg=0,end=len(string) )</td>
<td>类似于 find()函数，不过是从右边开始查找.</td>
</tr>
<tr>
<td>string.rindex( str, beg=0,end=len(string))</td>
<td>类似于 index()，不过是从右边开始.</td>
</tr>
<tr>
<td>string.rjust(width)</td>
<td>返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串</td>
</tr>
<tr>
<td>string.rpartition(str)</td>
<td>类似于 partition()函数,不过是从右边开始查找.</td>
</tr>
<tr>
<td>string.rstrip()</td>
<td>删除 string 字符串末尾的空格.</td>
</tr>
<tr>
<td>string.split(str=””, num=string.count(str))</td>
<td>以 str 为分隔符切片 string，如果 num有指定值，则仅分隔 num 个子字符串</td>
</tr>
<tr>
<td>string.splitlines([keepends])</td>
<td>按照行(‘\r’, ‘\r\n’, \n’)分隔，返回一个包含各行作为元素的列表，如果参数 keepends 为 False，不包含换行符，如果为 True，则保留换行符。</td>
</tr>
<tr>
<td>string.startswith(obj, beg=0,end=len(string))</td>
<td>检查字符串是否是以 obj 开头，是则返回 True，否则返回 False。如果beg 和 end 指定值，则在指定范围内检查.</td>
</tr>
<tr>
<td>string.strip([obj])</td>
<td>在 string 上执行 lstrip()和 rstrip()</td>
</tr>
<tr>
<td>string.swapcase()</td>
<td>翻转 string 中的大小写</td>
</tr>
<tr>
<td>string.title()</td>
<td>返回”标题化”的 string,就是说所有单词都是以大写开始，其余字母均为小写(见 istitle())</td>
</tr>
<tr>
<td>string.translate(str, del=””)</td>
<td>根据 str 给出的表(包含 256 个字符)转换 string 的字符,要过滤掉的字符放到 del 参数中</td>
</tr>
<tr>
<td>string.upper()</td>
<td>转换 string 中的小写字母为大写</td>
</tr>
<tr>
<td>string.zfill(width)</td>
<td>返回长度为 width 的字符串，原字符串 string 右对齐，前面填充0</td>
</tr>
<tr>
<td>string.isdecimal()</td>
<td>isdecimal()方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。</td>
</tr>
</tbody>
</table>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.5 Python 的数值和字符串/6. Python 的数值/">Python 的数值</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-Number（数字）"><a href="#Python-Number（数字）" class="headerlink" title="Python Number（数字）"></a>Python Number（数字）</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python Number 数据类型用于存储数值。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据类型是不允许改变的，这就意味着如果改变 Number 数据类型的值，将重新分配内存空间。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下实例在变量赋值时 Number 对象被创建：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">var1 = <span class="number">1</span></div><div class="line">var2 = <span class="number">10</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以使用 del 语句删除一些 Number 对象引用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;del 语句的语法是：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">del</span> var1[,var3[,var3[....,varN]]]]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以通过使用 del 语句删除单个或多个对象：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">del</span> var</div><div class="line"><span class="keyword">del</span> var_a, var_b</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 支持四种不同的数值类型</p>
<ul>
<li><strong>整型（int）</strong>-通常被称为是整型或整数，是正或负整数，不带小数点。</li>
<li><strong>长整型(long integers)</strong>-无限大小的整数，证书最后是一个大写或小写的L。</li>
<li><strong>浮点型（floating point real values）</strong>-浮点型由整数部分与小数部分组成，浮点型也可以使用科学计数法表示 （<code>$2.5e2=2.5x10^2=250$</code>）</li>
<li><strong>复数（complex numbers）</strong>-复数由实数部分和叙述部分构成，可以用 a + bj ,或者 complex(a,b) 表示，复数的实部 a 和虚部 b 都是浮点型。</li>
</ul>
<table>
<thead>
<tr>
<th>int</th>
<th>long</th>
<th>float</th>
<th>complex</th>
</tr>
</thead>
<tbody>
<tr>
<td>10</td>
<td>51924361L</td>
<td>0.0</td>
<td>3.14j</td>
</tr>
<tr>
<td>100</td>
<td>-0x1932L</td>
<td>15.20</td>
<td>45.j</td>
</tr>
<tr>
<td>-786</td>
<td>0122L</td>
<td>-21.9</td>
<td>9.322e-36j</td>
</tr>
<tr>
<td>080</td>
<td>0xDEFABCECBDAECBFBAEI</td>
<td>32.3+e18</td>
<td>.876j</td>
</tr>
<tr>
<td>-0490</td>
<td>535633629843L</td>
<td>-90.</td>
<td>-.6545+0j</td>
</tr>
<tr>
<td>-0x260</td>
<td>-052318172735L</td>
<td>-32.54e100</td>
<td>3e+26j</td>
</tr>
<tr>
<td>0x69</td>
<td>-472188529L</td>
<td>70.2-E12</td>
<td>4.53e-7j</td>
</tr>
</tbody>
</table>
<ul>
<li>长整型也可以使用小写 “L”，但是还是建议使用大写 “L”，避免与数字 “1” 混淆。Python 使用 “L” 来显示长整型。</li>
<li>Python 还支持复数，复数由实数部分和虚数部分构成，可以用 a + bj,或者complex(a,b)表示， 复数的实部a和虚部b都是浮点型。</li>
</ul>
<h2 id="Python-Number-类型转换"><a href="#Python-Number-类型转换" class="headerlink" title="Python Number 类型转换"></a>Python Number 类型转换</h2><table>
<thead>
<tr>
<th>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</th>
<th>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</th>
</tr>
</thead>
<tbody>
<tr>
<td>int(x [,base ])</td>
<td>将x转换为一个整数  </td>
</tr>
<tr>
<td>long(x [,base ])</td>
<td>将x转换为一个长整数  </td>
</tr>
<tr>
<td>float(x )</td>
<td>将x转换到一个浮点数  </td>
</tr>
<tr>
<td>complex(real [,imag ])</td>
<td>创建一个复数  </td>
</tr>
<tr>
<td>str(x )</td>
<td>将对象 x 转换为字符串  </td>
</tr>
<tr>
<td>repr(x )</td>
<td>将对象 x 转换为表达式字符串  </td>
</tr>
<tr>
<td>eval(str )</td>
<td>用来计算在字符串中的有效Python表达式,并返回一个对象  </td>
</tr>
<tr>
<td>tuple(s )</td>
<td>将序列 s 转换为一个元组  </td>
</tr>
<tr>
<td>list(s )</td>
<td>将序列 s 转换为一个列表  </td>
</tr>
<tr>
<td>chr(x )</td>
<td>将一个整数转换为一个字符  </td>
</tr>
<tr>
<td>unichr(x )</td>
<td>将一个整数转换为Unicode字符  </td>
</tr>
<tr>
<td>ord(x )</td>
<td>将一个字符转换为它的整数值  </td>
</tr>
<tr>
<td>hex(x )</td>
<td>将一个整数转换为一个十六进制字符串  </td>
</tr>
<tr>
<td>oct(x )</td>
<td>将一个整数转换为一个八进制字符串</td>
</tr>
</tbody>
</table>
<h2 id="Python-数学函数"><a href="#Python-数学函数" class="headerlink" title="Python 数学函数"></a>Python 数学函数</h2><table>
<thead>
<tr>
<th>函数</th>
<th>返回值（描述）</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/1.%20Python%20abs(" target="_blank" rel="external">abs(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回数字的绝对值，如abs(-10) 返回 10</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/2.%20Pyhton%20ceil(" target="_blank" rel="external">ceil(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回数字的上入整数，如math.ceil(4.1) 返回 5</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/3.%20Pyhton%20cmp(" target="_blank" rel="external">cmp(x, y)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>如果 x &lt; y 返回 -1, 如果 x == y 返回 0, 如果 x &gt; y 返回 1</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/4.%20Python%20exp(" target="_blank" rel="external">exp(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/5.%20Python%20fabs(" target="_blank" rel="external">fabs(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回数字的绝对值，如math.fabs(-10) 返回10.0</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/6.%20Python%20floor(" target="_blank" rel="external">floor(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回数字的下舍整数，如math.floor(4.9)返回 4</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/7.%20Python%20log(" target="_blank" rel="external">log(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>如math.log(math.e)返回1.0,math.log(100,10)返回2.0</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/8.%20Python%20log10(" target="_blank" rel="external">log10(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回以10为基数的x的对数，如math.log10(100)返回 2.0</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/9.%20Python%20max(" target="_blank" rel="external">max(x1, x2,…)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回给定参数的最大值，参数可以为序列。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/10.%20Python%20min(" target="_blank" rel="external">min(x1, x2,…)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回给定参数的最小值，参数可以为序列。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/11.%20Python%20modf(" target="_blank" rel="external">modf(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回x的整数部分与小数部分，两部分的数值符号与x相同，整数部分以浮点型表示。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/12.%20Python%20pow(" target="_blank" rel="external">pow(x, y)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>x**y 运算后的值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/13.%20Python%20round(" target="_blank" rel="external">round(x [,n])</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回浮点数x的四舍五入值，如给出n值，则代表舍入到小数点后的位数。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/14.%20Python%20sqrt(" target="_blank" rel="external">sqrt(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回数字x的平方根，数字可以为负数，返回类型为实数，如math.sqrt(4)返回 2+0j</td>
</tr>
</tbody>
</table>
<h2 id="Python-随机数函数"><a href="#Python-随机数函数" class="headerlink" title="Python 随机数函数"></a>Python 随机数函数</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;随机数可以用于数学、游戏、安全等领域中，还经常被嵌入到算法中，用以提高算法效率，并提高程序的安全性。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 包含以下常用随机数函数：</p>
<table>
<thead>
<tr>
<th>函数</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/1.%20Python%20choice(" target="_blank" rel="external">choice(seq)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>从序列的元素中随机挑选一个元素，比如random.choice(range(10))，从0到9中随机挑选一个整数。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/2.%20Python%20randrange(" target="_blank" rel="external">randrange ([start,] stop [,step])</a>%E5%87%BD%E6%95%B0.md)</td>
<td>从指定范围内，按指定基数递增的集合中获取一个随机数，基数缺省值为1</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/3.%20Python%20random(" target="_blank" rel="external">random()</a>%E5%87%BD%E6%95%B0.md)</td>
<td>随机生成下一个实数，它在[0,1)范围内。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/4.%20Python%20seed(" target="_blank" rel="external">seed([x])</a>%E5%87%BD%E6%95%B0.md)</td>
<td>改变随机数生成器的种子seed。如果你不了解其原理，你不必特别去设定seed，Python会帮你选择seed。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/5.%20Python%20shuffle(" target="_blank" rel="external">shuffle(lst)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>将序列的所有元素随机排序</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/6.%20Pyhton%20uniform(" target="_blank" rel="external">uniform(x, y)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>随机生成下一个实数，它在[x,y]范围内。</td>
</tr>
</tbody>
</table>
<h2 id="Python-三角函数"><a href="#Python-三角函数" class="headerlink" title="Python 三角函数"></a>Python 三角函数</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 包括以下三角函数：</p>
<table>
<thead>
<tr>
<th>函数</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/1.%20Python%20acos(" target="_blank" rel="external">acos(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回x的反余弦弧度值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/2.%20Python%20asin(" target="_blank" rel="external">asin(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回x的反正弦弧度值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/3.%20Python%20atan(" target="_blank" rel="external">atan(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回x的反正切弧度值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/4.%20Python%20atan2(" target="_blank" rel="external">atan2(y, x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回给定的 X 及 Y 坐标值的反正切值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/5.%20Python%20cos(" target="_blank" rel="external">cos(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回x的弧度的余弦值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/6.%20Python%20hypot(" target="_blank" rel="external">hypot(x, y)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回欧几里德范数 sqrt(x<em>x + y</em>y)。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/7.%20Python%20sin(" target="_blank" rel="external">sin(x)</a>%20%E5%87%BD%E6%95%B0.md)</td>
<td>返回的x弧度的正弦值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/8.%20Python%20tan(" target="_blank" rel="external">tan(x)</a>%20%E5%87%BD%E6%95%B0.md)</td>
<td>返回x弧度的正切值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/9.%20Python%20degrees(" target="_blank" rel="external">degrees(x)</a>%20%E5%87%BD%E6%95%B0.md)</td>
<td>将弧度转换为角度,如degrees(math.pi/2) ， 返回90.0</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/10.%20Python%20radians(" target="_blank" rel="external">radians(x)</a>%20%E5%87%BD%E6%95%B0.md)</td>
<td>将角度转换为弧度</td>
</tr>
</tbody>
</table>
<h2 id="Python-数学常量"><a href="#Python-数学常量" class="headerlink" title="Python 数学常量"></a>Python 数学常量</h2><table>
<thead>
<tr>
<th>常量</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>pi</td>
<td>数学常量 pi （圆周率，一般以π来表示）</td>
</tr>
<tr>
<td>e</td>
<td>数学常量 e ，e 即自然常数（自然常数）</td>
</tr>
</tbody>
</table>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.4 Python 的变量/5. Python 的变量/">Python 的变量</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-的变量"><a href="#Python-的变量" class="headerlink" title="Python 的变量"></a>Python 的变量</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;变量是计算机内存中的一块区域，变量可以存储规定范围内的值，而且值可以改变。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;python下变量是对一个数据的引用。</p>
<h2 id="变量的命名："><a href="#变量的命名：" class="headerlink" title="变量的命名："></a>变量的命名：</h2><ol>
<li>变量名由字母、数字、下划线组成。</li>
<li>变量不能以数字开头</li>
<li>不可以使用关键字</li>
<li>a a1 _a，这些都是合法的变量名<br> 表达式是将不同的数据（包括变量、函数）用运算符号按一定规则连接起来的一种式子。</li>
</ol>
<h3 id="Python对象的三要素：id、type、value"><a href="#Python对象的三要素：id、type、value" class="headerlink" title="Python对象的三要素：id、type、value"></a>Python对象的三要素：id、type、value</h3><ul>
<li>id：是对象的唯一标识，是对象在内存中的存储地址</li>
<li>type：是对象的数据类型</li>
<li>value：是对象的值，是对象在内存中存放的数据</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有一个name，理解为变量名</p>
<p>###变量的赋值：<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;变量的声明和定义的过程</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">1</span>]: a=<span class="number">1</span></div><div class="line"> </div><div class="line">In [<span class="number">2</span>]: id(a)</div><div class="line">Out[<span class="number">2</span>]: <span class="number">13933672</span></div><div class="line"> </div><div class="line">In [<span class="number">3</span>]: id(a)</div><div class="line">Out[<span class="number">3</span>]: <span class="number">13933672</span></div><div class="line"> </div><div class="line">In [<span class="number">4</span>]: a=<span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">5</span>]: id(a)</div><div class="line">Out[<span class="number">5</span>]: <span class="number">13933648</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;id（a）表示变量a在内存里的位置，a的变量值发生改变，在内存中的位置也发生改变。</p>
<h2 id="Python-运算符"><a href="#Python-运算符" class="headerlink" title="Python 运算符"></a>Python 运算符</h2><h3 id="1-算数运算符"><a href="#1-算数运算符" class="headerlink" title="1.算数运算符"></a>1.算数运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;+，一个值加上另一个值，也可以是一个字符串加上另一个字符串</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">38</span>]: <span class="number">3</span> + <span class="number">4</span></div><div class="line">Out[<span class="number">38</span>]: <span class="number">7</span></div><div class="line"> </div><div class="line">In [<span class="number">39</span>]: <span class="string">'a'</span> + <span class="string">'b'</span></div><div class="line">Out[<span class="number">39</span>]: <span class="string">'ab'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-，一个值减去另一个值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">40</span>]: <span class="number">5</span> - <span class="number">4</span></div><div class="line">Out[<span class="number">40</span>]: <span class="number">1</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;*，一个值乘以另一个值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">44</span>]: <span class="number">3</span> * <span class="number">4</span></div><div class="line">Out[<span class="number">44</span>]: <span class="number">12</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/，一个值除以另一个值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">45</span>]: <span class="number">4</span> / <span class="number">3</span></div><div class="line">Out[<span class="number">45</span>]: <span class="number">1</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：这里只取整数，如果想要小数点，那么除法运算的两个数值至少一个要是浮点数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">46</span>]: <span class="number">4.0</span> / <span class="number">3</span></div><div class="line">Out[<span class="number">46</span>]: <span class="number">1.3333333333333333</span></div><div class="line"> </div><div class="line">In [<span class="number">47</span>]: <span class="number">4</span> / <span class="number">3.0</span></div><div class="line">Out[<span class="number">47</span>]: <span class="number">1.3333333333333333</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//，只取整除，数值带上小数点也只取整数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">48</span>]: <span class="number">4.0</span> // <span class="number">3</span></div><div class="line">Out[<span class="number">48</span>]: <span class="number">1.0</span></div><div class="line"> </div><div class="line">In [<span class="number">49</span>]: <span class="number">4</span> // <span class="number">3.0</span></div><div class="line">Out[<span class="number">49</span>]: <span class="number">1.0</span></div><div class="line"> </div><div class="line">In [<span class="number">50</span>]: <span class="number">4</span> // <span class="number">3</span></div><div class="line">Out[<span class="number">50</span>]: <span class="number">1</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;%，表示取余</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">51</span>]: <span class="number">4</span> % <span class="number">3</span></div><div class="line">Out[<span class="number">51</span>]: <span class="number">1</span></div><div class="line"> </div><div class="line">In [<span class="number">52</span>]: <span class="number">5</span> % <span class="number">3</span></div><div class="line">Out[<span class="number">52</span>]: <span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">53</span>]: <span class="number">6</span> % <span class="number">3</span></div><div class="line">Out[<span class="number">53</span>]: <span class="number">0</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;**，表示乘方</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">54</span>]: <span class="number">2</span> ** <span class="number">3</span></div><div class="line">Out[<span class="number">54</span>]: <span class="number">8</span></div><div class="line"> </div><div class="line">In [<span class="number">55</span>]: <span class="number">2</span> ** <span class="number">10</span></div><div class="line">Out[<span class="number">55</span>]: <span class="number">1024</span></div></pre></td></tr></table></figure>
<h3 id="2-关系（比较）运算符"><a href="#2-关系（比较）运算符" class="headerlink" title="2.关系（比较）运算符"></a>2.关系（比较）运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;，大于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">56</span>]: <span class="number">2</span> &gt; <span class="number">1</span></div><div class="line">Out[<span class="number">56</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">57</span>]: <span class="number">1</span> &gt; <span class="number">2</span></div><div class="line">Out[<span class="number">57</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;，小于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">58</span>]: <span class="number">2</span> &lt; <span class="number">3</span></div><div class="line">Out[<span class="number">58</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">59</span>]: <span class="number">3</span> &lt; <span class="number">2</span></div><div class="line">Out[<span class="number">59</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&gt;=，大于等于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">60</span>]: <span class="number">2</span> &gt;= <span class="number">1</span></div><div class="line">Out[<span class="number">60</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">61</span>]: <span class="number">2</span> &gt;= <span class="number">2</span></div><div class="line">Out[<span class="number">61</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">62</span>]: <span class="number">2</span> &gt;= <span class="number">3</span></div><div class="line">Out[<span class="number">62</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;=，小于等于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">63</span>]: <span class="number">2</span> &lt;=<span class="number">3</span></div><div class="line">Out[<span class="number">63</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">64</span>]: <span class="number">3</span> &lt;= <span class="number">3</span></div><div class="line">Out[<span class="number">64</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">65</span>]: <span class="number">4</span> &lt;= <span class="number">3</span></div><div class="line">Out[<span class="number">65</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;==，恒等于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">72</span>]: <span class="number">1</span> == <span class="number">1</span></div><div class="line">Out[<span class="number">72</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">73</span>]: <span class="number">1</span> == <span class="number">2</span></div><div class="line">Out[<span class="number">73</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;!=，不等于</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">74</span>]: <span class="number">1</span> != <span class="number">2</span></div><div class="line">Out[<span class="number">74</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">75</span>]: <span class="number">2</span> != <span class="number">2</span></div><div class="line">Out[<span class="number">75</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<h3 id="3-赋值运算符"><a href="#3-赋值运算符" class="headerlink" title="3.赋值运算符"></a>3.赋值运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;=，表示直接给变量赋值，如果是字符串必须加上引号</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">11</span>]: x = <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">12</span>]: y = <span class="string">'abc'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：可以通过 type（）命令查看变量是数字还是字符串</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">15</span>]: type(x)</div><div class="line">Out[<span class="number">15</span>]: int</div><div class="line"> </div><div class="line">In [<span class="number">16</span>]: type(y)</div><div class="line">Out[<span class="number">16</span>]: str</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;+=，表示变量 的值再加上一个值（a=a+b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">13</span>]: x += <span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">14</span>]: x</div><div class="line">Out[<span class="number">14</span>]: <span class="number">5</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-=，表示变量的值再减去一个值（a=a-b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">17</span>]: x -= <span class="number">1</span></div><div class="line"> </div><div class="line">In [<span class="number">18</span>]: x</div><div class="line">Out[<span class="number">18</span>]: <span class="number">4</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;*=，表示变量的值再乘以一个值（a=a*b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">19</span>]: x *= <span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">20</span>]: x</div><div class="line">Out[<span class="number">20</span>]: <span class="number">8</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/=，表示变量的值再除以一个值（a=a/b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">21</span>]: x /= <span class="number">4</span></div><div class="line"> </div><div class="line">In [<span class="number">22</span>]: x</div><div class="line">Out[<span class="number">22</span>]: <span class="number">2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;%=，表示取余，变量被一个数整除后，还剩多少值（a=a%b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">29</span>]: x=<span class="number">10</span></div><div class="line"> </div><div class="line">In [<span class="number">30</span>]: x %= <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">31</span>]: x</div><div class="line">Out[<span class="number">31</span>]: <span class="number">1</span></div><div class="line"> </div><div class="line">In [<span class="number">32</span>]: x=<span class="number">9</span></div><div class="line"> </div><div class="line">In [<span class="number">33</span>]: x %= <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">34</span>]: x</div><div class="line">Out[<span class="number">34</span>]: <span class="number">0</span></div><div class="line"> </div><div class="line">In [<span class="number">35</span>]: x=<span class="number">11</span></div><div class="line"> </div><div class="line">In [<span class="number">36</span>]: x %= <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">37</span>]: x</div><div class="line">Out[<span class="number">37</span>]: <span class="number">2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;**=，表示乘方，a的b次方（a=a**b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">66</span>]: x = <span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">67</span>]: x **= <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">68</span>]: x</div><div class="line">Out[<span class="number">68</span>]: <span class="number">8</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//=，表示整除，（a=a//b）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">68</span>]: x</div><div class="line">Out[<span class="number">68</span>]: <span class="number">8</span></div><div class="line"> </div><div class="line">In [<span class="number">69</span>]: x //= <span class="number">3</span></div><div class="line"> </div><div class="line">In [<span class="number">70</span>]: x</div><div class="line">Out[<span class="number">70</span>]: <span class="number">2</span></div></pre></td></tr></table></figure>
<h3 id="4-逻辑运算符"><a href="#4-逻辑运算符" class="headerlink" title="4.逻辑运算符"></a>4.逻辑运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;and，逻辑与：true and false。表示一个成立and另一个也成立，返回值是 true</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">77</span>]: <span class="number">3</span> &gt; <span class="number">2</span> <span class="keyword">and</span> <span class="number">1</span> &lt; <span class="number">2</span></div><div class="line">Out[<span class="number">77</span>]: <span class="keyword">True</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果其中一个不成立，返回值是 false</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">78</span>]: <span class="number">1</span> == <span class="number">2</span> <span class="keyword">and</span> <span class="number">2</span> &gt; <span class="number">1</span></div><div class="line">Out[<span class="number">78</span>]: <span class="keyword">False</span></div><div class="line"> </div><div class="line">In [<span class="number">79</span>]: <span class="number">1</span> &lt; <span class="number">2</span> <span class="keyword">and</span> <span class="number">1</span> != <span class="number">1</span></div><div class="line">Out[<span class="number">79</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;or，逻辑或：false or true，表示只要有一个成立那么返回就是true</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">80</span>]: <span class="number">1</span> &lt; <span class="number">2</span> <span class="keyword">or</span> <span class="number">1</span> != <span class="number">1</span></div><div class="line">Out[<span class="number">80</span>]: <span class="keyword">True</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;not，逻辑非：not true，表示取反</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">81</span>]: <span class="keyword">not</span> <span class="number">1</span> &lt; <span class="number">2</span></div><div class="line">Out[<span class="number">81</span>]: <span class="keyword">False</span></div><div class="line"> </div><div class="line">In [<span class="number">82</span>]: <span class="keyword">not</span> <span class="number">1</span> != <span class="number">1</span></div><div class="line">Out[<span class="number">82</span>]: <span class="keyword">True</span></div></pre></td></tr></table></figure>
<h3 id="5-身份运算符"><a href="#5-身份运算符" class="headerlink" title="5.身份运算符"></a>5.身份运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;身份运算符用来判断两个变量的引用对象是否指向统一个内存的对象，即 id(varibale1)?=id(variable2)。</p>
<ul>
<li>is：判断两个标识符是不是引用自一个对象</li>
<li>is not：判断两个标识符是不是引用不同对象</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">88</span>]: a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</div><div class="line"> </div><div class="line">In [<span class="number">89</span>]: b = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</div><div class="line"> </div><div class="line">In [<span class="number">90</span>]: <span class="keyword">print</span> ( a == b )</div><div class="line"><span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">91</span>]: <span class="keyword">print</span> ( a <span class="keyword">is</span> b )</div><div class="line"><span class="keyword">False</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为变量 a 和变量 b 的 value 是一样的， == 运算符比较的变量的 value ，所以返回true。is 是判断两个变量是否引用同一个对象，也就是比较的是 id ，所以返回 false。</p>
<h3 id="6-成员关系运算符"><a href="#6-成员关系运算符" class="headerlink" title="6.成员关系运算符"></a>6.成员关系运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;成员运算符能够判断一个指定对象是否是作为一个容器中的元素，由此来判断两个对象间的关系。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;容器：包含了其他对象的引用的数据类型。</p>
<ul>
<li>in：当一个对象存在一个容器中时为 true</li>
<li>not in：当一个对象不在一个容器中时为 true</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">93</span>]: a = <span class="number">1</span></div><div class="line"> </div><div class="line">In [<span class="number">94</span>]: b = <span class="number">2</span></div><div class="line"> </div><div class="line">In [<span class="number">95</span>]: c = [<span class="number">1</span>,<span class="number">3</span>,<span class="number">4</span>]</div><div class="line"> </div><div class="line">In [<span class="number">96</span>]: a <span class="keyword">in</span> c</div><div class="line">Out[<span class="number">98</span>]: <span class="keyword">True</span></div><div class="line"> </div><div class="line">In [<span class="number">97</span>]: b <span class="keyword">in</span> c</div><div class="line">Out[<span class="number">99</span>]: <span class="keyword">False</span></div></pre></td></tr></table></figure>
<h3 id="7-位运算符"><a href="#7-位运算符" class="headerlink" title="7.位运算符"></a>7.位运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 Python 中 |、&amp;、^、~等运算符的意思不在于处理逻辑关系，而是二进制数据的为运算，数字以二进制形式的补码存储和计算，以原码结果来显示。若数字为正值，它的补码就是原码本身、若为负值，则它的补码为源码减1再按位取反。两个数字的计算本质是两个二进制补码的计算。</p>
<ul>
<li>|：按位或运算符，只要对应的二个二进位有一个为1时，结果位就为1.</li>
<li>&amp;：按位与运算符，参与运算的两个值，如果两个相应的位都为1，则该位为的结果为1，否则为0</li>
<li>^：按位异或运算符，当两对应的二进位相异时，结果为1</li>
<li>~：按位取反运算符，对数据的每个二进制位取反，即把1变为0，把0变为1</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">a = <span class="number">0011</span> <span class="number">1100</span></div><div class="line"> </div><div class="line">b = <span class="number">0000</span> <span class="number">1101</span></div><div class="line"> </div><div class="line">-----------------</div><div class="line"> </div><div class="line">a&amp;b = <span class="number">0000</span> <span class="number">1100</span></div><div class="line"> </div><div class="line">a|b = <span class="number">0011</span> <span class="number">1101</span></div><div class="line"> </div><div class="line">a^b = <span class="number">0011</span> <span class="number">0001</span></div><div class="line"> </div><div class="line">~a = <span class="number">1100</span> <span class="number">0011</span></div></pre></td></tr></table></figure>
<h3 id="8-位移运算符"><a href="#8-位移运算符" class="headerlink" title="8.位移运算符"></a>8.位移运算符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;位移运算符是非常有效率的计算方法之一，在对数学运算和对程序执行效率要求高的程序中推荐使用。</p>
<ul>
<li>&lt;&lt;：左移动运算符，运算数的各二进位全部左移若干位，由 “&lt;&lt;” 右边的数值指定移动的位数，高位丢弃，低位补0</li>
<li><blockquote>
<blockquote>
<p>：右移动运算符，把 “&gt;&gt;” 左边的运算数的各二进位全部右移若干位， “&gt;&gt;” 右边的数指定移动的位数</p>
</blockquote>
</blockquote>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">a = <span class="number">0011</span> <span class="number">1100</span></div><div class="line"> </div><div class="line">a &lt;&lt; <span class="number">2</span> <span class="number">1111</span> <span class="number">0000</span> <span class="comment">#结果为240</span></div><div class="line"> </div><div class="line">a &gt;&gt; <span class="number">2</span> <span class="number">0000</span> <span class="number">1111</span> <span class="comment">#结果为15</span></div></pre></td></tr></table></figure>
<h2 id="Python运算符优先级"><a href="#Python运算符优先级" class="headerlink" title="Python运算符优先级"></a>Python运算符优先级</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下标列出了从最高到最低优先级的所有运算符：</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>**</td>
<td>指数 (最高优先级)</td>
</tr>
<tr>
<td>~ + -</td>
<td>按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)</td>
</tr>
<tr>
<td>* / % //</td>
<td>乘，除，取模和取整除</td>
</tr>
<tr>
<td>+ -</td>
<td>加法减法</td>
</tr>
<tr>
<td>&gt;&gt; &lt;&lt;</td>
<td>右移，左移运算符</td>
</tr>
<tr>
<td>&amp;</td>
<td>位 ‘AND’</td>
</tr>
<tr>
<td>^ \</td>
<td></td>
<td>位运算符</td>
</tr>
<tr>
<td>&lt;= &lt; &gt; &gt;=</td>
<td>比较运算符</td>
</tr>
<tr>
<td>&lt;&gt; == !=</td>
<td>等于运算符</td>
</tr>
<tr>
<td>= %= /= //= -= += <em>= *</em>=</td>
<td>赋值运算符</td>
</tr>
<tr>
<td>is is not</td>
<td>身份运算符</td>
</tr>
<tr>
<td>in not in</td>
<td>成员运算符</td>
</tr>
<tr>
<td>not or and</td>
<td>逻辑运算符</td>
</tr>
</tbody>
</table>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下实例演示了python所有运算符优先级的操作：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line">  </div><div class="line">a = <span class="number">20</span></div><div class="line">b = <span class="number">10</span></div><div class="line">c = <span class="number">15</span></div><div class="line">d = <span class="number">5</span></div><div class="line">e = <span class="number">0</span></div><div class="line">  </div><div class="line">e = (a + b) * c / d <span class="comment">#( 30 * 15 ) / 5</span></div><div class="line"><span class="keyword">print</span> <span class="string">"(a + b) * c / d 运算结果为："</span>, e</div><div class="line">  </div><div class="line">e = ((a + b) * c) / d <span class="comment"># (30 * 15 ) / 5</span></div><div class="line"><span class="keyword">print</span> <span class="string">"((a + b) * c) / d 运算结果为："</span>, e</div><div class="line">  </div><div class="line">e = (a + b) * (c / d); <span class="comment"># (30) * (15/5)</span></div><div class="line"><span class="keyword">print</span> <span class="string">"(a + b) * (c / d) 运算结果为："</span>, e</div><div class="line">  </div><div class="line">e = a + (b * c) / d; <span class="comment"># 20 + (150/5)</span></div><div class="line"><span class="keyword">print</span> <span class="string">"a + (b * c) / d 运算结果为："</span>, e</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">(a + b) * c / d 运算结果为： <span class="number">90</span></div><div class="line">((a + b) * c) / d 运算结果为： <span class="number">90</span></div><div class="line">(a + b) * (c / d) 运算结果为： <span class="number">90</span></div><div class="line">a + (b * c) / d 运算结果为： <span class="number">50</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.3 Python 的文件类型/4. Python 的文件类型/">Python 的文件类型</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-的文件类型"><a href="#Python-的文件类型" class="headerlink" title="Python 的文件类型"></a>Python 的文件类型</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 的使用我们可以用解释器交互方式 python 和 ipython，也可以建立一个程序文件。</p>
<h2 id="1-源代码"><a href="#1-源代码" class="headerlink" title="1.源代码"></a>1.源代码</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 源代码文件以 “py” 为扩展名，由 Python 程序解释，不需要编译。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># vim 1.py</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写入内容</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="keyword">print</span> <span class="string">'hello world'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：第一行必须写  <code>#!/usr/bin/python</code> ，因为第一行表示用什么解释器来解释文件，如果不写就是以 bash 解释。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># python 1.py</span></div><div class="line">hello world</div><div class="line">[root@192 day01]<span class="comment"># chmod u+x 1.py </span></div><div class="line">[root@192 day01]<span class="comment"># ./1.py</span></div><div class="line">hello world</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 的执行和 bash 很相似。</p>
<h2 id="2-字节代码"><a href="#2-字节代码" class="headerlink" title="2.字节代码"></a>2.字节代码</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 源码文件经编译后生成的扩展名为 “pyc” 的文件</p>
<h3 id="编译方法："><a href="#编译方法：" class="headerlink" title="编译方法："></a>编译方法：</h3><blockquote>
<p>import py_compile<br>py_compile.compile(‘hello.py’)</p>
</blockquote>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># vim 2.py</span></div></pre></td></tr></table></figure>
<p>写入内容</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"> </div><div class="line"><span class="keyword">import</span> py_compile</div><div class="line"> </div><div class="line">py_compile.compile(<span class="string">'1.py'</span>)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># python 2.py</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;发现执行以后什么输出都没有</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B/01.png?raw=true" alt="iamges"><figcaption class="figure__caption">iamges</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ls 查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B/02.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;发现多了一个 1.pyc ，这个 1.pyc 就是对 1.py 进行编译<br>  后产生。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看文件类型</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># file 1.pyc</span></div><div class="line">1.pyc: python 2.6 byte-compiled</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;显示被字节编译过<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看 1.pyc 文件，发现是二进制文件</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B/03.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># python 1.pyc</span></div><div class="line">hello world</div></pre></td></tr></table></figure>
<h2 id="3-优化代码"><a href="#3-优化代码" class="headerlink" title="3.优化代码"></a>3.优化代码</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;经过优化的源码文件，扩展名为 “pyo”</p>
<h3 id="编译方法：-1"><a href="#编译方法：-1" class="headerlink" title="编译方法："></a>编译方法：</h3><blockquote>
<p>python -o -m py_compile hello.py<br>-O 表示优化<br>-m 表示模块</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># python -O -m py_compile 1.py</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B/04.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;生成一个 1.pyo 的文件。1.pyo 也是经过编译的，只是多了有一个优化。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># python 1.pyo</span></div><div class="line">hello world</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不想让别人查看源码文件就可以把源码文件编译成 pyc 或 pyo 的文件，不过建议使用源码文件。</p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/20">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
